﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using BFTest.Particles;

namespace BFTest.Ammunitions
{
    class CRocket : CBullet
    {
        static Vector2 glowSize = new Vector2(15, 8);

        Vector2 lastPosition;

        Vector2 size = new Vector2(4, 2);
        public override Vector2 Size { get { return size; } set { size = value; } }

        float speed = 20f;
        public override float Speed { get { return speed; } set { speed = value; } }

        // Rocket specific speed values
        float aliveTime = 0f;
        const float accelRate = 5f;
        const float maxSpeed = 500f;

        // Flame values
        TimeSpan flameLast;
        static TimeSpan flameRate = TimeSpan.FromMilliseconds(10);
        static TimeSpan flameLifeTime = TimeSpan.FromMilliseconds(100);
        static float flameRange = MathHelper.ToRadians(40);
        static float flameVelocity = 50;
        static int flameCount = 3;      // Number of flame particles spawned per tick.
        
        int damage = 100;
        public override int Damage { get { return damage; } set { damage = value; } }

        Color bulletColor = Color.Red;
        public override Color BulletColor { get { return bulletColor; } set { bulletColor = value; } }

        public override BulletType BulletType { get { return BulletType.Rocket; } }

        public CRocket(int originID, Vector2 position, Vector2 direction, int id, string weapon)
            : base(originID, position, direction, id, weapon)
        {
            this.Initialize();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // todo: benchmark to see if it's faster to downcast to float, or just work with doubles
            aliveTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (Velocity.LengthSquared() < maxSpeed * maxSpeed)
            {
                Vector2 accelleration = Direction * accelRate * aliveTime;
                Velocity += accelleration;
            }

            // Spawn flame particles
            if (gameTime.TotalGameTime - flameLast >= flameRate)
            {
                Vector2 vel = -Velocity;
                vel.Normalize();
                vel *= flameVelocity;
                for (int x = 0; x < flameCount; x++)
                {
                    float offset = (float)((BFTestGame.Battlefield.Rand.NextDouble() * flameRange) - (flameRange / 2));
                    BFTestGame.Particles.Add(new CParticleFire(Position, vel.RotateVector(offset), flameLifeTime));
                }
                flameLast = gameTime.TotalGameTime;
            }

            lastPosition = Position;

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 glowOffset = Vector2.Normalize(Position - lastPosition) * -(glowSize.X/3);
            float glowAngle = (float)Math.Atan2(glowOffset.Y, glowOffset.X);
            Rectangle glowRect = new Rectangle(
                (int)(Position.X + glowOffset.X),
                (int)(Position.Y + glowOffset.Y),
                (int)glowSize.X, (int)glowSize.Y);

            spriteBatch.Draw(BFTestGame.ActiveBullets.BulletTexture, BoundingBox, BulletColor);
        }
    }
}
